package com.landscope.issue.service.applet;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.landscope.issue.bean.E_PERMISSION_TYPE;
import com.landscope.issue.controller.api.dto.DownLoadReportDto;
import com.landscope.issue.controller.api.param.DownloadReportParam;
import com.landscope.issue.controller.base.ResponseResult;
import com.landscope.issue.controller.wx.dto.AppletReportListDto;
import com.landscope.issue.controller.wx.param.AppletReportListParam;
import com.landscope.issue.entity.Project;
import com.landscope.issue.mapper.ProjectMapper;
import com.landscope.issue.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.*;

@Service
@Slf4j
public class ApiAppletReportService {
    private String reportUrl;


    @Resource
    private ProjectMapper projectMapper;

    public ResponseResult<AppletReportListDto> downloadReport(AppletReportListParam param) {
        // 验证token
        TokenUtil.verifyToken();

        // 验证功能权限
        TokenUtil.verifyRole(E_PERMISSION_TYPE.vbad_en.toString());

        // 验证项目权限
        TokenUtil.verifyAuthority(param.getAccessToken(), param.getProjectCode());

        List<AppletReportListDto> list = new ArrayList<>();

        // 获取根据项目获取文件夹下
        Project project = projectMapper.findByCode(param.getProjectCode());
        reportUrl = project.getAppletUrl();
        String path = project.getProjectReseve();
        File file = new File(path);
        if (file.exists()) {
            // 如果文件夹存在
            File fs[] = file.listFiles();
            for (File f : fs) {
                if (!f.isDirectory()) {
                    AppletReportListDto downLoadReportDto = new AppletReportListDto();
                    // 获取创建时间

                    Path path2 = Paths.get(f.getAbsolutePath());
                    BasicFileAttributes attrs = null;
                    try {
                        attrs = Files.readAttributes(path2, BasicFileAttributes.class);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // 从基本属性类中获取文件创建时间
                    FileTime fileTime = attrs.lastModifiedTime();
                    // 将文件创建时间转成毫秒
                    long createdTime = fileTime.toMillis();

                    downLoadReportDto.setDatetime(DateUtil.formatDateTime(new Date(createdTime)));
                    downLoadReportDto.setName(f.getName());
                    long size = FileUtil.size(f);
                    BigDecimal b1 = new BigDecimal(size);
                    downLoadReportDto.setSize(b1.divide(new BigDecimal(1048576)).setScale(1, BigDecimal.ROUND_HALF_UP).toString() + "MB");
                    downLoadReportDto.setUrl(reportUrl + "/" + project.getCode() + "/report/" + f.getName());
                    downLoadReportDto.setOrderBy(param.getOrderby());
                    list.add(downLoadReportDto);
                }
            }
        }
        Collections.sort(list);
        return new ResponseResult().success().setData(list);
    }
}
